<template>
  <div class="overview-card" :class="{ 'is-up': isPositiveGrowth, 'is-down': !isPositiveGrowth }">
    <div class="overview-card__icon">
      <i :class="iconClass"></i>
    </div>
    <div class="overview-card__content">
      <h3 class="overview-card__title">{{ title }}</h3>

      <div class="overview-card__data">{{ value || 0 }}</div>

      <!-- <div class="overview-card__growth" :class="growthClass">
        <i :class="growthIconClass"></i>
        <span>{{ formattedGrowth }}</span>
      </div> -->
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue';

interface OverviewCardProps {
  title: string;
  value: number | string;
  growth: number;
  icon: string;
  formatter?: (value: number | string) => string;
  currency?: boolean;
}

const props = withDefaults(defineProps<OverviewCardProps>(), {
  formatter: undefined,
  currency: false
});

const isPositiveGrowth = computed(() => Number(props.growth) >= 0);

const growthClass = computed(() => ({
  'overview-card__growth--up': isPositiveGrowth.value,
  'overview-card__growth--down': !isPositiveGrowth.value
}));

const growthIconClass = computed(() =>
  isPositiveGrowth.value ? 'fas fa-arrow-up' : 'fas fa-arrow-down'
);

const iconClass = computed(() => {
  // 确保图标名称正确，移除前缀如果已包含
  const iconName = props.icon.startsWith('fa-') ? props.icon : `fa-${props.icon}`;
  return `fas ${iconName}`;
});

const formattedValue = computed(() => {
  if (props.formatter) {
    return props.formatter(props.value);
  }

  if (props.currency) {
    return typeof props.value === 'number'
      ? `¥${props.value.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`
      : props.value;
  }

  return typeof props.value === 'number'
    ? props.value.toLocaleString('zh-CN')
    : props.value;
});

const formattedGrowth = computed(() => {
  const absGrowth = Math.abs(Number(props.growth));
  return `${absGrowth.toFixed(1)}%`;
});

// 确保图标正确加载
onMounted(() => {
  if (typeof window !== 'undefined' && window.FontAwesome) {
    window.FontAwesome.dom.watch();
  }
});
</script>

<style lang="scss" scoped>
.overview-card {
  display: flex;
  padding: 20px;
  background: linear-gradient(145deg, #ffffff, #f8fafc);
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.03);
  transition: all 0.3s ease;
  height: 100%;
  border: 1px solid rgba(226, 232, 240, 0.7);
  position: relative;
  overflow: hidden;
  max-width: 100%;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  }

  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 6px;
    height: 100%;
    background: linear-gradient(to bottom, #3b82f6, #38bdf8);
    opacity: 0.8;
  }

  &.is-up:before {
    background: linear-gradient(to bottom, #10b981, #14b8a6);
  }

  &.is-down:before {
    background: linear-gradient(to bottom, #ef4444, #f87171);
  }

  &__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 54px;
    height: 54px;
    border-radius: 12px;
    font-size: 22px;
    margin-right: 18px;
    background-color: rgba(244, 245, 247, 0.8);
    color: #3b82f6;
    transition: all 0.3s ease;
    flex-shrink: 0;
    box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.03);

    /* 确保图标显示正确 */
    i {
      display: inline-block;
      width: auto;
      height: auto;
      line-height: 1;
      font-style: normal;
      text-align: center;
    }
  }

  &.is-up &__icon {
    background-color: rgba(16, 185, 129, 0.08);
    color: #10b981;
  }

  &.is-down &__icon {
    background-color: rgba(239, 68, 68, 0.08);
    color: #ef4444;
  }

  &__content {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-width: 0;
    width: 100%;
  }

  &__title {
    margin: 0 0 8px;
    font-size: 15px;
    font-weight: 500;
    color: #64748b;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &__data {
    font-size: 26px;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 8px;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &__growth {
    font-size: 14px;
    display: flex;
    align-items: center;
    padding: 4px 8px;
    border-radius: 4px;
    width: fit-content;
    max-width: 100%;

    i {
      margin-right: 4px;
      flex-shrink: 0;
      width: auto;
      height: auto;
      line-height: 1;
      font-style: normal;
    }

    span {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    &--up {
      background-color: rgba(16, 185, 129, 0.1);
      color: #10b981;
    }

    &--down {
      background-color: rgba(239, 68, 68, 0.1);
      color: #ef4444;
    }
  }
}

@media (max-width: 768px) {
  .overview-card {
    padding: 16px;

    &__icon {
      width: 48px;
      height: 48px;
      font-size: 20px;
      margin-right: 12px;
    }

    &__title {
      font-size: 14px;
    }

    &__data {
      font-size: 22px;
    }

    &__growth {
      font-size: 12px;
    }
  }
}

/* IE11 兼容性修复 */
@media screen and (-ms-high-contrast: active),
(-ms-high-contrast: none) {
  .overview-card {
    display: -ms-flexbox;
    -ms-flex-direction: row;

    &__icon {
      -ms-flex: 0 0 60px;
    }

    &__content {
      -ms-flex: 1 1 auto;
      display: -ms-flexbox;
      -ms-flex-direction: column;
    }

    &__growth {
      width: auto;
    }
  }
}
</style>
